<%--
 *  File:    DataCleanPeriodDetail.jsp
 *  @Author: Jamesli
 *  Date:    2010/06/21
 *
 *  Copyright (C) 2000 - 2010 Oval Technologies, Inc.
 *  All rights reserved. 
 *
 *  This software is the confidential and proprietary information of Oval 
 *  Technologies, Inc. ("Confidential Information").  You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Oval.
--%>


<%@ include file="/inc/SessionTimeout.inc"%>
<%@ page language="java" %>     
<%@ page import="java.util.*" %>
<%@ page import="org.apache.log4j.Category" %>
<%@ page import="java.text.DateFormat,java.text.SimpleDateFormat"%>

<%@ page contentType = "text/html;charset=UTF-8" %>
<%@ taglib uri="/WEB-INF/PageFlow.tld" prefix="PageFlow" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/custom.tld" prefix="custom" %>

<% //--------------------------------------  setting logger --%>
<%
  	Category cat = Category.getInstance("jsp.DataCleanPeriodDetail"); 
%>

<%
  //init param
  String strModuleName = "CM";
  String privilegeCode = "E0131";
  String contextPath = request.getContextPath();
%>
<% //--------------------------------------  include files list --%>
<%@ include file="/inc/Security.inc" %>
<%@ include file="/inc/PageDefault.inc" %>
<%@ include file="/inc/LocationDefault.inc" %>

<html>
<head>
<title>DataCleanPeriodDetail</title>
<PageFlow:CheckPageFlow pageName="p_datacleanperioddetail"/>
<% 
  	String iscontinuum = (String)pageContext.getAttribute("iscontinuum");
  	
  	 Integer periodstatus = (Integer)pageContext.getAttribute("periodstatus");
  	 if(periodstatus == null){
  	 	periodstatus = new Integer(-1);
  	 }
  	 
%>
<link rel="stylesheet" href="<%=contextPath %>/css/<%= strLanguageCode %>/main.css" type="text/css">
<script language="JavaScript" src="<%=contextPath %>/css/<%= strLanguageCode %>/script.js"></script>
<script language="JavaScript" src="<%=contextPath %>/js/ScriptLib.js"></script>
<%@ include file="/inc/NewCalendar.inc" %>
<script>
function page_checkDate()
{   
	var rowNums = Number(document.cform.numrows.value);
	if( rowNums == 0 )
	{
		return true;
	} 
	if('<%=iscontinuum %>' == 'Y')
	{
		return true;
	}
	
	var startDate = document.cform.startdate;
	var endDate = document.cform.enddate;
	var firstStartDate = document.cform.startdate1;
	var firstEndDate = document.cform.enddate1;
	var lastEndDate = eval('document.cform.enddate' + rowNums);
	
	if(rowNums == 1)
	{   
		if(startDate.value > firstEndDate.value){return true;}
		if(endDate.value < firstStartDate.value){return true;}

		return false;
	}
	if(rowNums > 1)
	{
		if(startDate.value > lastEndDate.value){return true;}
		if(endDate.value < firstStartDate.value){return true;}
			
		for(var i=1 ;i<rowNums ; i++)
		{
			var leftOpt = eval('document.cform.enddate'+i);
			var rightOpt = eval('document.cform.startdate'+(Number(i)+1));
			if(startDate.value>leftOpt.value && endDate.value<rightOpt.value) continue;
			return false;
		}
		
		return true;
	}
}

function clickRow(id,rownum)
{
  document.cform.detailid.value = id;
  document.cform.currentrow.value = rownum;
}
function page_delete()
{
  var periodid = document.cform.datacleanperiodid.value;
  var detailid = document.cform.detailid.value;

  if(periodid == null || periodid.length == 0 || periodid == "" || periodid == "-1")
    return;
  if(detailid == null || detailid.length == 0 || detailid == "" || detailid == "-1")
    return;
    
  if(!confirm("<%= message.getMessageDesc("ARE_YOU_SURE_DEL")%>")) return;
  if( document.cform.numrows.value == 0 )return;
	
	var idx = document.cform.currentrow.value;
	var status = eval('document.cform.fakestatus' + idx);
	
	if( status.value != '0' )
	{
		alert('<%=message.getMessageDesc("CANNOT_DELETE_PERIOD_IN_USE")%>');
		return;
	}
	if( document.cform.numrows.value > 1 )
	{
		if( idx < document.cform.numrows.value )
		{
			alert('<%=message.getMessageDesc("CAN_ONLY_DEL_LAST_PERIOD")%>')
			return;
		}
	} 
  
  cform.target = "_self";
  document.cform.actionname.value = "delete";
  isRunning = true;
  document.cform.submit();
  
}

function page_add(){
	if(!checkForm(cform))return;
	var startdate = document.cform.startdate.value;
	var enddate = document.cform.enddate.value;
	if(startdate>enddate){
		 alert("<%= message.getMessageDesc("STARTDATE_CAN_NOT_LATE_THAN_ENDDATE")%>");
		 return;
	}
	if(!page_checkDate()){
		 alert("<%= message.getMessageDesc("PERIOD_CAN_NOT_DUPLICATE")%>");
		 return;
	}
	cform.periodno.value = cform.periodno.value;
	cform.startdate.value = cform.startdate.value;
	cform.enddate.value = cform.enddate.value;
	cform.target = "_self";
	document.cform.actionname.value = "add";
	isRunning = true;
	document.cform.submit();
}

function page_startup(){
	cform.target = "_self";
	document.cform.actionname.value = "startup";
	isRunning = true;
	document.cform.submit();
}
</script>
</head>

<body>
<div style="height: 250px;overflow: auto;">
<table width="100%" cellspacing="0" border="0" cellpadding="0">
 <form name="cform" method="post" action="<%=contextPath %>/Dispatcher"  target="_parent">
    <input type="hidden" name="pagename" value="p_datacleanperioddetail">
    <input type="hidden" name="actionname">
    <custom:hidden property="periodid" changedName="datacleanperiodid"/>
    <custom:hidden property="reloadflag"/>
  <tr> 
    <td> 

      <table width="100%" border="1" cellpadding="0" bordercolorlight="#999999" bordercolordark="#FFFFFF" cellspacing="0" align="center">
         <tr  class="deepblue-left"> 
            <td width="4%" class="title_center">&nbsp;</td>
            <td class="title_center"><%= label.getLabelName("L_SerialNo") %></td>
            <td width="25%" class="title_center"><%= label.getLabelName("L_PeriodStartDate") %></td>
            <td width="25%" class="title_center"><%= label.getLabelName("L_PeriodEndDate") %></td>
            <td width="20%" class="title_center"><%= label.getLabelName("L_UseStatus") %></td>
         </tr>
         <%
             int j = 0;
             String strClassType = new String(); 
             Integer nDefaultDetailId = new Integer(-1);    
             boolean bHasChecked = false;
             Vector details = (Vector)pageContext.getAttribute("details");
             if(details == null) details = new Vector();
               
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date lastEndDate = new Date();
			Date newStartDate = new Date();
			String newstartdate = "";
			boolean newstartdatereadonly = false;
			
			if("Y".equalsIgnoreCase(iscontinuum)){
			    if( details.size() == 0 ){
			    	lastEndDate = new Date();	
			    }else{
			    	HashMap lastHash = (HashMap)details.get(details.size()-1);
				    		   
				    if( lastHash == null ){
				        lastHash = new HashMap();
				    }
				    if( lastHash.get("enddate") != null ){
				        lastEndDate = (Date)lastHash.get("enddate");
				    }
				
				    Calendar c1 = Calendar.getInstance();
				    c1.setTime(lastEndDate);
				    c1.add(Calendar.DATE, 1);
				    newStartDate = c1.getTime();  
				    newstartdatereadonly = true;
			    }
			    newstartdate = df.format(newStartDate);
			}
          %>
          <logic:iterate id="detail" collection="<%= details %>">                      
         <%
            HashMap detailMap = (HashMap)pageContext.getAttribute("detail");
            if(detailMap == null) detailMap = new HashMap();
            String strIsChecked = new String("");
            //redefine status
            String fakestatus = "";
            
            Integer status = (Integer)detailMap.get("status");
            if(status != null){
            	if(status.intValue() == 27){
            		fakestatus = "0";
            	}else if(status.intValue() == 43){
            		fakestatus = "1";
            	}else if(status.intValue() == 99){
            		fakestatus = "2";
            	}
            }
            
            if(j%2 == 1){
              strClassType = "td-listrom";             
            }else{                    
              strClassType = "";
            }
            
           	if(!bHasChecked)
            {
              strIsChecked = "Checked";                       
              bHasChecked = true;
              nDefaultDetailId = (Integer)detailMap.get("dcperioddetailid");      
            }
            j++;     
          %>
           <tr onmouseover="this.className='tr-over'" onmouseout="this.className='<%=JspHelper.filterNull(strClassType)%>'"  onClick="javascript:radiotrue('radio<%=JspHelper.filterNull(j)%>');clickRow('<%=JspHelper.filterNull( detailMap.get("dcperioddetailid") )%>','<%=j %>')" class="<%=JspHelper.filterNull(strClassType)%>">
            <td>
              <input class="normalradio" id="radio<%=JspHelper.filterNull(j )%>" type="radio" name="checkdept" <%=JspHelper.filterNull( strIsChecked)%> >
            </td>
            <td>&nbsp;<custom:write property="periodno" name="detail"/></td>
            <td>&nbsp;<custom:write property="startdate" name="detail"/>
            	<custom:hidden property="startdate" name="detail" colIndex="<%=new Integer(j) %>"/>
            </td>
            <td>&nbsp;<custom:write property="enddate" name="detail"/>
            	<custom:hidden property="enddate" name="detail" colIndex="<%=new Integer(j) %>"/>
            </td>
            <td class="td_center"><custom:write property="status" name="detail" dictType="Status" nbsp="true"/></td>
            	<custom:hidden property="fakestatus" value="<%=fakestatus %>" colIndex="<%=new Integer(j) %>"/>
          </tr>
          </logic:iterate>
          <%//---------------set default value-------------------- %>
          <input type="hidden" name="detailid" value="<%=JspHelper.filterNull( nDefaultDetailId )%>">
          <input type="hidden" name="numrows" value="<%=JspHelper.filterNull( j )%>">
          <input type="hidden" name="currentrow" value="1">
          <%
          	  int sj = 0;
                  sj = j + 1;
              String stj= "";
                  stj = sj + "";
          %>
           <tr>
            <td>&nbsp;</td>
            <td>&nbsp;<custom:write property="periodno" value="<%=stj %>"/></td>
            <input type="hidden" name="periodno" value="<%=sj %>">	   
          <%if(newstartdatereadonly){%>
            <td>&nbsp;<custom:write property="startdate" dataType="date" dataFormat="yyyy-MM-dd" value="<%=newstartdate %>"/>
            <custom:hidden property="startdate" value="<%=newstartdate %>"/></td>	
          <%} else {%>
          	<td>&nbsp;<custom:text property="startdate" dataType="date" dataFormat="yyyy-MM-dd" label="L_PeriodStartDate" command="notnull" value="<%=newstartdate %>"/> <a onclick="javascript:c.show(this,'cform.startdate','<%=JspHelper.filterNull( session.getAttribute("dateformat") )%>')"><custom:button type="3" label="L_Asterisk" imgsrc="Time.gif" border="0"/></a></td>
          <%}%>            
            <td>&nbsp;<custom:text property="enddate" dataType="date" dataFormat="yyyy-MM-dd" label="L_PeriodEndDate" command="notnull"/> <a onclick="javascript:c.show(this,'cform.enddate','<%=JspHelper.filterNull( session.getAttribute("dateformat") )%>')"><custom:button type="3" label="L_Asterisk" imgsrc="Time.gif" border="0"/></a></td>
            <td>&nbsp;</td>
          </tr>
      </table>
      
      
    </td>
  </tr>
 </form>
</table>
</div>
<table width="100%" cellspacing="0" cellpadding="0">
        <tr> 
          <td class="summary_top"></td>
        </tr>
        <tr> 
          <td class="td_center">
            <a onclick="page_add()"><custom:button type="1" label="L_Add" imgsrc="new.gif" border="0" style="width:60"/></a>
            <a onclick="page_delete()"><custom:button type="1" label="L_Delete" imgsrc="delete.gif" border="0" style="width:60"/></a>
            <%if(periodstatus.intValue() == 10 && j > 0){ %>
            	<a onclick="page_startup()"><custom:button type="1" label="L_Startup" imgsrc="delete.gif" border="0" style="width:60"/></a>
            <%} %>
          </td>
        </tr>
      </table>
<script language="javascript">
	if(document.cform.reloadflag.value == 'Y')
	{
		document.cform.reloadflag.value="";
		var strLocation = parent.up.location.href;
		
		var id = document.cform.datacleanperiodid.value;
		//alert(strLocation);
		parent.up.document.cform.dcperiodid.value = id;
		parent.up.page_summary('jumppage');
		//parent.up.location.href="DataCleanPeriodSummary.jsp?<%= OvalTools.randomURL() %>&periodid=" + id;
		//parent.up.location.reload();
	}
</script>
</body>
</html>


